ENTRYPOINT = 0x30400
ENTRYOFFSET = 0x400

ASM = nasm
DASM = ndisasm
CC = gcc
LD = ld
ASMBFLAGS = -I boot/include/
ASMKFLAGS = -I include/ -I include/sys/ -f elf32
CFLAGS = -I include/ -I include/sys/ -c -fno-builtin -m32 -fno-stack-protector
LDFLAGS = -s -Ttext $(ENTRYPOINT) -m elf_i386 -Map krnl.map
DASMFLAGS = -D

ORANGESBOOT = boot/boot.bin boot/loader.bin
ORANGESKERNEL = kernel.bin
OBJS = kernel/kernel.o kernel/systask.o kernel/start.o kernel/main.o \
	kernel/clock.o kernel/keyboard.o kernel/tty.o \
	kernel/i8259.o kernel/global.o kernel/protect.o\
	kernel/proc.o kernel/console.o kernel/hd.o \
	lib/klib.o lib/kliba.o \
	lib/string.o lib/printf.o lib/vsprintf.o \
	lib/syscall.o lib/misc.o  lib/open.o lib/close.o  \
	lib/read.o lib/write.o \
	fs/main.o fs/open.o fs/misc.o fs/read_write.o
DASMOUTPUT = kernel.bin.asm

.PHONY : everything final image clean realclean disasm all buildimg

everything : $(ORANGESBOOT) $(ORANGESKERNEL)

all : realclean everything

final : all clean

image : final createimg buildimg

clean :
	rm -f $(OBJS)
	
realclean : 
	rm -f $(OBJS) $(ORANGESBOOT) $(ORANGESKERNEL)
	
disasm:
	$(DASM) $(DASMFLAGS) $(ORANGESKERNEL) > $(DASMOUTPUT)
	
createimg:
	dd if=/dev/zero of=a.img bs=1474560 count=1
	
buildimg :
	dd if=boot/boot.bin of=a.img bs=512 count=1 conv=notrunc
	sudo mount -o loop a.img /mnt/floppy
	sudo cp -fv boot/loader.bin /mnt/floppy/
	sudo cp -fv kernel.bin /mnt/floppy/
	sudo umount /mnt/floppy
	
boot/boot.bin : boot/boot.asm boot/include/load_inc.asm boot/include/fat12hdr.asm
	$(ASM) $(ASMBFLAGS) -o $@ $<
	
boot/loader.bin : boot/loader.asm boot/include/load_inc.asm \
		boot/include/fat12hdr.asm boot/include/pm.inc
	$(ASM) $(ASMBFLAGS) -o $@ $<
	
$(ORANGESKERNEL) : $(OBJS)
	$(LD) $(LDFLAGS) -o $(ORANGESKERNEL) $(OBJS)
	
kernel/kernel.o : kernel/kernel.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
	
kernel/start.o : kernel/start.c
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/clock.o : kernel/clock.c
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/main.o : kernel/main.c 
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/i8259.o : kernel/i8259.c 
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/global.o : kernel/global.c 
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/protect.o : kernel/protect.c
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/proc.o : kernel/proc.c
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/keyboard.o : kernel/keyboard.c
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/tty.o : kernel/tty.c
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/console.o : kernel/console.c
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/systask.o : kernel/systask.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/klib.o : lib/klib.c 
	$(CC) $(CFLAGS) -o $@ $<
	
kernel/hd.o : kernel/hd.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/kliba.o : lib/kliba.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
	
lib/string.o : lib/string.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
	
lib/misc.o : lib/misc.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/syscall.o : lib/syscall.asm
	$(ASM) $(ASMKFLAGS) -o $@ $<
	
lib/printf.o : lib/printf.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/vsprintf.o : lib/vsprintf.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/open.o : lib/open.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/close.o : lib/close.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/read.o : lib/read.c
	$(CC) $(CFLAGS) -o $@ $<
	
lib/write.o : lib/write.c
	$(CC) $(CFLAGS) -o $@ $<
	
fs/main.o : fs/main.c
	$(CC) $(CFLAGS) -o $@ $<
	
fs/open.o : fs/open.c
	$(CC) $(CFLAGS) -o $@ $<
	
fs/misc.o : fs/misc.c
	$(CC) $(CFLAGS) -o $@ $<
	
fs/read_write.o : fs/read_write.c
	$(CC) $(CFLAGS) -o $@ $<